
package com.huaweicloud.saaslanding.install.properties;

import static com.huaweicloud.saaslanding.install.constant.ModelConstant.NACOS_PREFIX;

import com.huaweicloud.saaslanding.basic.enumeration.ServiceManageFrameTypeEnum;
import com.huaweicloud.saaslanding.basic.utils.CheckNullUtil;
import com.huaweicloud.saaslanding.basic.utils.RegexUtil;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Nacos配置
 *
 * @since 2022-10-27
 */
@EqualsAndHashCode
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Validated
@ConfigurationProperties(prefix = NACOS_PREFIX)
public class Nacos {

    @Value("${saaslanding-install.config-discovery-frame.type}")
    private String configType;

    private String namespace;

    private String nacosMode;

    private String nacosServers;

    private String preferHostMode;

    private Integer nacosServerPort;

    private String nacosServerIp;

    private String name;

    private String password;

    public String getNamespace() {
        if (Objects.equals(ServiceManageFrameTypeEnum.NACOS.getName(), configType)
            && CheckNullUtil.isNotNull(namespace)) {
            return namespace;
        } else {
            throw new IllegalArgumentException("未启用nacos或未配置命名空间(nacos.namespace)");
        }
    }

    public String getPreferHostMode() {
        if (Objects.equals(ServiceManageFrameTypeEnum.NACOS.getName(), configType)
            && CheckNullUtil.isNotNull(preferHostMode)) {
            return preferHostMode;
        } else {
            throw new IllegalArgumentException("未启用nacos或未配置支持IP还是域名模式(nacos.preferHostMode)");
        }
    }

    public Integer getNacosServerPort() {
        if (Objects.equals(ServiceManageFrameTypeEnum.NACOS.getName(), configType)
            && CheckNullUtil.isNotNull(nacosServerPort)) {
            return nacosServerPort;
        } else {
            throw new IllegalArgumentException("未启用nacos或未配置运行端口(nacos.nacosServerPort)");
        }
    }

    private static final String SPIT = ";";

    public List<String> getNacosServers() {
        if (Objects.equals(ServiceManageFrameTypeEnum.NACOS.getName(), configType)
            && CheckNullUtil.isNotNull(namespace)) {
            nacosServers = nacosServers.replaceAll(SPIT, StrUtil.SPACE);
            return new ArrayList<>(Arrays.asList(nacosServers.split(StrUtil.SPACE))).stream()
                .map(RegexUtil::ofHttp)
                .collect(Collectors.toList());
        } else {
            throw new IllegalArgumentException("未启用nacos或未配置集群地址(nacos.nacosServers)");
        }
    }
}
